package cn.stylefeng.guns.modular.controller;


import cn.hutool.core.bean.BeanUtil;
import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.entity.User;
import cn.stylefeng.guns.modular.service.impl.UserCompanyServiceImpl;
import cn.stylefeng.guns.sys.core.exception.enums.BizExceptionEnum;
import cn.stylefeng.guns.sys.core.log.LogObjectHolder;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import cn.stylefeng.roses.kernel.model.exception.ServiceException;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author XiangTianGe
 * @since 2020-04-07
 */
@Controller
@RequestMapping("company")
public class UserController extends BaseController {

    private static String PREFIX = "/company/";

    @Autowired
    private UserCompanyServiceImpl userCompanyService;

    /**
     * 跳转到用户的首页
     */
    @RequestMapping
    public String index() {
        return PREFIX + "company.html";
    }

    /**
     * 跳转到用户的详情
     */
    @RequestMapping("details/{id}")
    public String details(@PathVariable("id") Long id, Model model) {
        User user = userCompanyService.getById(id);
        model.addAllAttributes(BeanUtil.beanToMap(user));
        LogObjectHolder.me().set(user);
        return PREFIX + "company_details.html";
    }

    /**
     * 查询注册用户列表
     */
    @ResponseBody
    @GetMapping("list")
    public LayuiPageInfo list(String timeLimit, String keyword) {
        //获取分页参数
        Page page = LayuiPageFactory.defaultPage();

        //拼接查询条件
        String beginTime = null;
        String endTime = null;

        if (ToolUtil.isNotEmpty(timeLimit)) {
            String[] split = timeLimit.split(" - ");
            beginTime = split[0];
            endTime = split[1];
        }
        //根据条件查询日志
        return userCompanyService.getList(page, beginTime, endTime, keyword);
    }

    /**
     * 下级列表
     */
    @ResponseBody
    @RequestMapping("junior-list/{userId}")
    public LayuiPageInfo juniorList(@PathVariable Long userId) {
        //获取分页参数
        Page page = LayuiPageFactory.defaultPage();
        //根据条件查询日志
        return userCompanyService.juniorList(page, userId);
    }

    /**
     * 冻结用户
     */
    @ResponseBody
    @RequestMapping("freeze")
    public ResponseData freeze(Long userId) {
        if (ToolUtil.isEmpty(userId)) {
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }
        this.userCompanyService.freeze(userId);
        return SUCCESS_TIP;
    }

    /**
     * 解冻用户
     */
    @ResponseBody
    @RequestMapping("unfreeze")
    public ResponseData unfreeze(Long userId) {
        if (ToolUtil.isEmpty(userId)) {
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }
        this.userCompanyService.unfreeze(userId);
        return SUCCESS_TIP;
    }

    /**
     * 新增用户
     */
    @RequestMapping("add")
    public String add() {
        return PREFIX + "company_add.html";
    }

    /**
     * 新增服务公司- （用户）
     */
    @ResponseBody
    @RequestMapping("add-company")
    public ResponseData addCompany(@RequestBody User user) {
        return userCompanyService.addCompany(user);
    }


}

